package br.sed.theenemy.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;

import br.sed.theenemy.business.exception.DBException;
import br.sed.theenemy.model.bean.Contact;
import br.sed.theenemy.model.bean.User;

/**
 * 
 * @author JulioCB
 * @email jcbjulio@gmail.com
 * 
 */
public class ContactDao extends AbstractDao{
	
	// Unica instancia da classe
	protected static ContactDao instanceDao;
	
	public void create(Contact contact) throws DBException{
		String sql = "insert into contacts (the_id, id_source, id_target) values " +
						 "("+getId()+"," +
						 "'"+contact.getSourceId()+"', " +
						 "'"+contact.getTargetId()+"')";			
		//System.out.println("Log:"+sql);
		executeQuery(sql, "Error: Insert Contact");
	}
	
	public List<User> findUsersBySourceId(Long sourceId, Integer start, Integer max) throws SQLException, ParseException, DBException{
		String sql = "select * from contacts c inner join user u on c.id_target = u.id " +
						"where c.id_source = "+ sourceId +" limit "+ max +" offset "+ start;
		ResultSet result = null;
		result = getRS(sql);		
		List<User> userList = UserDao.fillUserList(result);
		result.close();
		closeConnection();		
		return userList;
	}
	
	public List<User> findAllUsersBySourceId(Long sourceId) throws SQLException, ParseException, DBException{
		String sql = "select * from contacts c inner join user u on c.id_target = u.id " +
						"where c.id_source = "+ sourceId;
		ResultSet result = null;
		result = getRS(sql);
		
		List<User> userList = UserDao.fillUserList(result);
		result.close();
		closeConnection();
		return userList;
	}

//	public Contact findBySrsTrg(Long sourceId, Long targetId) throws NumberFormatException, SQLException, InvalidUniqueBeanException{
//		String sql = "select * from contacts where id_source = " + sourceId + " and id_target = " + targetId;
//		ResultSet result = null;
//
//		result = getRS(sql);
//		
//		Contact contact = getContact(result);
//		
//		return contact;
//	}
	
//	public User findUserBySrsTrg(Long sourceId, Long targetId) throws NumberFormatException, SQLException, InvalidUniqueBeanException, ParseException{
//		String sql = "select * from contacts where id_source = " + sourceId + " and id_target = " + targetId;
//		ResultSet result = null;
//
//		result = getRS(sql);
//		
//		User user = UserDao.getUser(result);
//		
//		return user;
//	}
	
	public Integer countBySourceId(Long sourceId) throws SQLException, DBException{
		String sql = "select count(1) total from contacts where id_source = "+ sourceId;
		ResultSet result = null;
		
		result = getRS(sql);
		
		result.next();
		int total = result.getInt("total");
		result.close();
		closeConnection();
		return total;
	}
	
	public void delete(Long sourceId, Long targetId) throws DBException{
		String sql = "delete from contacts where id_source = " + sourceId + " and id_target = " + targetId;
		executeQuery(sql, "Error: Delete Contact");
	}
	
//	private List<Contact> fillContactList(ResultSet result) throws NumberFormatException, SQLException{
//		List<Contact> contactList = new ArrayList<Contact>();
//		while(result.next()){
//			Contact contact = new Contact();
//			contact.setId(Long.parseLong(result.getString("id")));
//			contact.setSourceId(Long.parseLong(result.getString("sourceId")));
//			contact.setTargetId(Long.parseLong(result.getString("targetId")));
//			contactList.add(contact);
//		}
//		return contactList;
//	}
	
//	private Contact getContact(ResultSet result) throws NumberFormatException, SQLException, InvalidUniqueBeanException{
//		List<Contact> contactList = fillContactList(result);
//		if(contactList.size()==1){
//			return contactList.get(0);	
//		}else{
//			throw new InvalidUniqueBeanException("Error getting user, not unique");
//		}
//	}
	
	public static ContactDao getInstance(){
		
		// Verifica se ja tem instancia
		if ( instanceDao == null ){			
			instanceDao = new ContactDao();			
		}
		
		return instanceDao;
		
	}
}
